$().ready(function() {
    validateRule();

    layui.use(['form'], function() {
        var form = layui.form;

        form.on('select(questionType)', function (data) {
            console.log('questionType',data.value);
            selectQuestionType()
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            console.log('data.field',data.field);
            if(data.field.questionType == 0 || data.field.questionType == 2){
                var sections = resetSection();
                console.log("sections",sections)
                data.field.answerSection = sections;
            }
            console.log('data.field  111',data.field)

            var url = ctx +"module/survey/question/" + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
            $.ajax({
                cache : true,
                type : "POST",
                url : url,
                data : data.field,// 你的formid
                async : false,
                error : function(request) {
                    parent.layer.alert("Connection error");
                },
                success : function(data) {
                    if (data.code == 0) {
                        parent.layer.msg("操作成功");
                        parent.reLoad();
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);

                    } else {
                        parent.layer.alert(data.msg)
                    }
                }
            });
            return false;
        });
    });
});

function delSection(index) {
    console.log("delSection index",index);
    $("#div_value_" + index).remove();
}

function addSection() {
    var section_now_index = parseInt($("#section_now_index").val());
    $("#section_now_index").val(section_now_index+1);
    var ss = "<div class=\"layui-form-item\" id=\"div_value_"+section_now_index+"\">\n" +
        "         <label class=\"layui-form-label\">选项"+(section_now_index + 1)+"</label>\n" +
        "         <div class=\"layui-input-block\" >\n" +
        "              <input id=\"value_"+section_now_index+"\" name=\"value_"+section_now_index+"\"\n" +
        "                      value=\"\" class=\"layui-input\" type=\"text\" style=\"width:82%;float: left\" />\n" +
        "                   <button onclick=\"delSection("+section_now_index+")\" class=\"layui-form-label\" style=\"float: left\">删除</button>\n" +
        "              </div>\n" +
        "     </div>";
    $("#divSections").append(ss);
}


function selectQuestionType() {
    var type = $("#questionType").val();
    console.log('type',type)
    $("#divSections").addClass("layui-hide");
    if(type == 0){//单选题
        $("#divSections").removeClass("layui-hide");
    }else if(type == 2){//多选题
        $("#divSections").removeClass("layui-hide");
    }
}


function resetSection(){
    var section_count = parseInt($("#section_count").val());
    var section_now_index = parseInt($("#section_now_index").val());
    console.log("section_count",section_count)
    console.log("section_now_index",section_now_index)

    var jsonAll = [];
    if(section_now_index > 0){
        var indexJson = 0;
        for(var i = 0; i < section_now_index;i++ ){
            var div = $("#div_value_" + i);
            console.log("#div_value_" + i);
            if(div.length > 0){
                console.log("#div_value_" + i +"存在");
                var value = $("#value_" + i).val();
                var json = { index :indexJson , value : value ,check :''};
                jsonAll.push(json);
                indexJson ++;
            }
        }
        console.log("jsonAll",JSON.stringify(jsonAll))
    }
    return JSON.stringify(jsonAll);
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            sort : {
                required : true
            }
        },
        messages : {
            sort : {
                required : icon + "请输入索引"
            }
        }
    })
}
